package fr.mywiki.view.web.struts.action.admin.structure;

import java.rmi.RemoteException;
import java.util.HashMap;

import javax.ejb.FinderException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import fr.mywiki.business.resources.ResourceManager;
import fr.mywiki.business.structure.StructureLite;
import fr.mywiki.business.structure.StructureManager;
import fr.mywiki.model.ejbAdapter.utils.PersistenceException;
import fr.mywiki.view.web.struts.action.ListAction;
import fr.mywiki.view.web.struts.action.admin.account.EditAccountAction;
import fr.mywiki.view.web.struts.action.structure.TreeStructureAction;
import fr.mywiki.view.web.struts.form.ListForm;
import fr.mywiki.view.web.struts.form.admin.structure.ManageStructureForm;
import fr.mywiki.view.web.struts.form.structure.SelectSingleStructController;

/**
 * Dispatch action associated to the structure management.
 * 
 * @author Thomas
 */
public class ManageStructureAction extends TreeStructureAction implements ListAction {
	/**
	 * Displays the structure.
	 */
	public ActionForward display(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse responde)
			throws RemoteException, FinderException {
		ManageStructureForm form = (ManageStructureForm) actionForm;
		form.setDisplayedRootName(ResourceManager.getMessage("structure.root.name",
				request.getLocale()));
		form.setSelectStructureController(SelectSingleStructController.createController(request));
		
		form.initConsultation();
		form.setLocale(request.getLocale());

		return mapping.findForward("display");
	}

	/** Displays the list of accounts on this structure element */
	public ActionForward manageAccounts(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws RemoteException, FinderException {
		ManageStructureForm form = (ManageStructureForm) actionForm;
		form.getSelectStructureController().centerTree(form.getSelectStructureController().getStructure());
		
		form.initManageElements();
		return mapping.findForward("display");
	}

	/** Launches the modification of the current element */
	public ActionForward modifyCurrent(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse responde) throws RemoteException, FinderException {
		ManageStructureForm form = (ManageStructureForm) actionForm;
		form.getSelectStructureController().centerTree(form.getSelectStructureController().getStructure());
		form.initModification();

		return mapping.findForward("display");
	}

	/** Launches the creation of a new element, child of the current one */
	public ActionForward initCreation(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws RemoteException, FinderException {
		ManageStructureForm form = (ManageStructureForm) actionForm;
		form.getSelectStructureController().centerTree(form.getSelectStructureController().getStructure());
		form.initCreation();

		return mapping.findForward("display");
	}

	/**
	 * Validates the modifications made on the current element or the newly
	 * created element.
	 */
	public ActionForward validateEdition(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse responde) throws RemoteException, FinderException,
			PersistenceException {
		ManageStructureForm form = (ManageStructureForm) actionForm;
		StructureLite struct = null;
		if (ManageStructureForm.EDITING_MODE.equals(form.getCurrentMode())) {
			struct = StructureManager.modifyStructure(form.getSelectStructureController()
					.getStructure(), form.getStructName());
		} else if (ManageStructureForm.CREATING_MODE.equals(form.getCurrentMode())) {
			struct = StructureManager.createStructure(form.getStructName(), form
					.getSelectStructureController().getStructure());
		}

		form.getSelectStructureController().setStructure(struct);
		form.getSelectStructureController().setTree(
				form.getSelectStructureController().loadTree(
						ResourceManager.getLocale(request)));
		form.centerTree(struct);
		form.initConsultation();

		return mapping.findForward("display");
	}

	/** Launches the modification of the current element */
	public ActionForward cancelEdition(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse responde) throws RemoteException, FinderException {
		ManageStructureForm form = (ManageStructureForm) actionForm;
		form.initConsultation();
		
		form.getSelectStructureController().centerTree(form.getSelectStructureController().getStructure());

		return mapping.findForward("display");
	}

	/** Removes the currently selected structure element. */
	public ActionForward removeCurrent(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws RemoteException, FinderException {
		ManageStructureForm form = (ManageStructureForm) actionForm;
		
		StructureLite tbr = form.getSelectStructureController().getStructure(); // to be removed
		StructureLite ntd = StructureManager.getParent(tbr); // next to display
		
		StructureManager.removeStructure(tbr);
		
		form.getSelectStructureController().setTree(form.getSelectStructureController().loadTree(ResourceManager.getLocale(request)));
		form.initConsultation();
		
		form.centerTree(ntd);
		
		return mapping.findForward("display");
	}
	
	/** Closes the elements list part */
	public ActionForward closeList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response)  {
		ManageStructureForm form = (ManageStructureForm) actionForm;
		form.setDisplayElementsList(false);
		return mapping.findForward("display");
	}
	
	protected HashMap buildElementMap(StructureLite structure,
			HttpServletRequest request) {
		HashMap res = new HashMap();
		if (structure == null)
			res.put("structName", ResourceManager.getMessage("structure.root.name",
					request.getLocale()));
		else
			res.put("structName",structure.getName());
		return res;
	}

	public ActionForward selectElement(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		ListForm form = (ListForm) actionForm;
		request.setAttribute(EditAccountAction.ACCOUNT_ID, new Long(form
				.getIdElement()));
		return mapping.findForward("editAccount");
	}
}
